package com.jiangdg.demo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Environment;

import com.jiangdg.ausbc.utils.ToastUtils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImageSaver {

    public static void saveImageToGallery(Context context, String imagePath) {
        Bitmap bitmap = BitmapFactory.decodeFile(imagePath);

        File imageFile = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES),  System.currentTimeMillis()+".jpg");

        try {
            FileOutputStream out = new FileOutputStream(imageFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 通知系统更新媒体库
        MediaScannerConnection.scanFile(context,
                new String[]{imageFile.getAbsolutePath()}, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                        // 图片已保存到相册
                        ToastUtils.show("保存到相册成功");
                    }
                });
    }

    public static void saveMp4ToGallery(Context context, String path) {

        File imageFile = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES),  System.currentTimeMillis()+".mp4");

        try {
            // 写入文件
            FileOutputStream out = new FileOutputStream(imageFile);
            byte[] videoData = getVideoData(path);  // 假设这是一个自定义方法，用于读取视频数据
            out.write(videoData);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 通知系统更新媒体库
        MediaScannerConnection.scanFile(context,
                new String[]{imageFile.getAbsolutePath()}, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                        // 图片已保存到相册
                        ToastUtils.show("保存到相册成功");
                    }
                });
    }


    private static byte[] getVideoData(String videoPath) throws IOException {
        // 这里是读取视频数据的逻辑
        // 例如，使用FileInputStream来读取视频文件
        return readBytes(videoPath);
    }

    private static byte[] readBytes(String path) throws IOException {
        File file = new File(path);
        byte[] bytesArray = new byte[(int) file.length()];
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
        bis.read(bytesArray, 0, bytesArray.length);
        bis.close();
        fis.close();
        return bytesArray;
    }
}